package com.gitee.l0km.codegen.base;

import java.util.Collections;
import java.util.Map;
import java.util.Objects;

import com.gitee.l0km.com4j.base.Assert;
import com.gitee.l0km.com4j.basex.TypeNameUtils.FullName;
import com.google.common.base.MoreObjects;

public class FullNameByImporList implements FullName{
	private final Map<String, Class<?>> importedList;
	/** output package */
	private String pkg;
	public FullNameByImporList(Map<String, Class<?>> importedList) {
		this(importedList, "");
	}
	public FullNameByImporList(Map<String, Class<?>> importedList,String pkg) {
		Assert.notNull(importedList, "importedList");
		this.importedList = Collections.unmodifiableMap(importedList);
		this.pkg = pkg;
	}
	@Override
	public boolean isFullName(Class<?> type) {
		Class<?> clazz = CodeGenUtils.getElementClass(type);
		Class<?> importedClass;
		if (clazz.isPrimitive()) {
			return true;
		} else if (CodeGenUtils.isJavaLangClass(clazz)) {
			return null != (importedClass = importedList.get(clazz.getSimpleName())) && importedClass != clazz;
		} else if(pkg.equals(clazz.getPackage().getName())){
			return false;
		}else{
			return !importedList.containsValue(clazz);
		}
	}
	public void setPkg(String pkg) {
		this.pkg = MoreObjects.firstNonNull(pkg, "");
	}
	@Override
	public int hashCode() {
		return Objects.hash(importedList, pkg);
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		FullNameByImporList other = (FullNameByImporList) obj;
		return Objects.equals(importedList, other.importedList) && Objects.equals(pkg, other.pkg);
	}
}